<?php

namespace app\common\model\merchant;


use app\common\model\BaseModel;
use think\db\exception\DbException;
use think\Exception;


/**
 * 商户订单表
 * Class Order
 * @package app\common\model
 */
class OrderModel extends BaseModel
{
    //
    protected $name = 'order';

    protected const  VIRTUAL_TYPE = 1;
    // 设置字段信息
    protected $schema = [
        'order_no' => 'string',        // 订单号
        'mer_id' => 'string',        // 所属商户
        'shop_id' => 'int',        // 店铺ID
        'memo' => 'string',        // 备注
        'op_user' => 'int',        // 操作人
        'create_at' => 'datetime',        // 创建时间
        'update_at' => 'datetime',        // 更新时间
    ];

    /**
     * 分页列表
     * @param array $where
     * @param       $page
     * @param       $cont
     * @return mixed
     * @throws DbException
     */
    public function orderPage(array $where, $page = '1', $cont = 30)
    {

        return $this->db()->alias('a')
            ->where($where ?? [])
            ->field('order_no,shop_id,sum_num,memo,ip,create_at,shop_id,op_user,status,status as status_name')->withAttr(['status_name' => function($value){
                $array = ['1' => '已确认' , '0' => '待确认'];
                return $array[$value] ?? '未知';
            }])
            ->order('create_at desc,order_no asc')
            ->paginate(['page' => $page, 'list_rows' => $cont]);
    }


    /**
     * 分页列表
     * @param  $order_no
     * @return mixed
     */
    public function orderInfo($order_no)
    {
        return $this->db()->where(['order_no' => $order_no])
            ->field('order_no,shop_id,sum_num,memo,ip,create_at,shop_id,op_user')->find();

    }

    /**
     * 添加
     * @param  $mer_id |商户
     * @param  $shop_id |店铺
     * @param  $memo |备注
     * @param  $op_user |操作人
     * @throws Exception
     */
    public function orderAdd($mer_id, $shop_id, $sum_num, $memo, $op_user, $status = 0)
    {
        $order_nos = make_unique_no(PRODUCT_CODE_TYPE_VIRTUAL);
        $order_no = $this->db()->insert([
            'order_no' => $order_nos,
            'mer_id' => $mer_id,
            'shop_id' => $shop_id,
            'memo' => $memo,
            'sum_num' => $sum_num,
            'op_user' => $op_user,
            'ip' => request()->ip(),
            'status' => $status
        ]);
        if (!$order_no) {
            throw  new Exception('创建失败', API_ERROR);
        }

        return $order_nos;
    }

    /**
     * 批量添加
     * @param $data
     * @param  $memo |备注
     * @param  $op_user |操作人
     * @param int $status
     * @throws Exception
     */
    public function orderAddAll($data , $memo, $op_user, $status = 0)
    {
        $ip = request()->ip();
        $arrays = [];
        foreach ($data as $v){
            $array = [
                'memo' => $memo,
                'sum_num' => $v['num'],
                'status' => $status,
                'ip' => $ip,
                'op_user' => $op_user,
            ];
            unset($v['num']);
            $arrays[] = array_merge($v,$array);
        }
        $order_no = $this->db()->insertAll($arrays);
        if (!$order_no) {
            throw  new Exception('创建失败', API_ERROR);
        }

    }



}
